package cz.data.domain.masterdata.service.impl;

import cz.data.common.core.DataConstant;
import cz.data.common.exception.DataException;
import cz.data.common.support.db.DbType;
import cz.data.common.support.db.core.DbTable;
import cz.data.common.support.db.driver.TableOperation;
import cz.data.domain.driver.rpc.ElasticsearchDriverFeign;
import cz.data.domain.driver.rpc.IDriverFeign;
import cz.data.domain.driver.rpc.JdbcDriverFeign;
import cz.data.domain.driver.rpc.MongoDriverFeign;
import cz.data.domain.masterdata.model.entity.ModelEntity;
import cz.data.domain.masterdata.service.ModelManage;
import cz.data.domain.masterdata.service.ModelService;
import cz.data.domain.masterdata.utils.ModelUtils;
import cz.data.domain.metadata.model.entity.MetadataSourceEntity;
import cz.data.domain.metadata.rpc.MetadataSourceFeign;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Properties;

@Service
public class ModelManageImpl implements ModelManage {

    @Resource
    ModelService modelService;
    @Resource
    MetadataSourceFeign metadataSourceFeign;

    @Resource
    JdbcDriverFeign jdbcDriverFeign;
    @Resource
    ElasticsearchDriverFeign elasticsearchDriverFeign;
    @Resource
    MongoDriverFeign mongoDriverFeign;

    @Override
    public void createTable(String id) {
        ModelEntity model = modelService.getModelById(id);
        if (DataConstant.TrueOrFalse.TRUE.getKey().equals(model.getIsSync())) {
            throw new DataException("重复建模");
        }
        String sourceId = model.getSourceId();
        MetadataSourceEntity source = metadataSourceFeign.getMetadataSourceById(sourceId);
        DbTable table = ModelUtils.dbTable(model);
        IDriverFeign driverFeign = getDriverFeign(source.getDbType());
        TableOperation operation = new TableOperation();
        operation.setSourceId(sourceId);
        operation.setTable(table);
        operation.setProperties(new Properties());
        Boolean success = driverFeign.createTable(operation);
        if (Boolean.TRUE.equals(success)) {
            createTablePost(model);
        } else {
            throw new DataException("模型表创建失败!");
        }
    }

    private void createTablePost(ModelEntity model) {
        ModelEntity update = new ModelEntity();
        update.setId(model.getId());
        update.setIsSync(DataConstant.TrueOrFalse.TRUE.getKey());
        update.setFlowStatus(DataConstant.AuditState.AGREE.getKey());
        modelService.updateById(update);
    }

    @Override
    public void dropTable(String id) {
        ModelEntity model = modelService.getModelById(id);
        if (Objects.isNull(model)) {
            throw new DataException("模型不存在");
        }
        String sourceId = model.getSourceId();
        MetadataSourceEntity source = metadataSourceFeign.getMetadataSourceById(sourceId);
        DbTable table = ModelUtils.dbTable(model);
        IDriverFeign driverFeign = getDriverFeign(source.getDbType());
        TableOperation operation = new TableOperation();
        operation.setSourceId(sourceId);
        operation.setTable(table);
        Boolean result = driverFeign.dropTable(operation);
        if (Boolean.TRUE.equals(result)) {
            modelService.deleteModelById(id);
        } else {
            throw new DataException("模型表删除失败!");
        }
    }

    private IDriverFeign getDriverFeign(String dbType) {
        if (DbType.ELASTICSEARCH.getDb().equals(dbType)) {
            return elasticsearchDriverFeign;
        } else if (DbType.MONGODB.getDb().equals(dbType)) {
            return mongoDriverFeign;
        } else {
            return jdbcDriverFeign;
        }
    }
}
